<?php
Class Project_model extends CI_Model
{
	public function insert($data)
	{
		if($this->db->insert('project_post', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	public function insertTES($data)
	{
		if($this->db->insert('tes', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	public function insertThread($data)
	{
		if($this->db->insert('project_thread', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	public function insertProject($data)
	{
		if($this->db->insert('project_parent', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	public function insertFile($data)
	{
		if($this->db->insert('project_file', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	public function insertPost($data)
	{
		if($this->db->insert('project_post', $data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function insertPostROW($data)
	{
		if($this->db->insert('row_comments', $data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function insertApproval($data)
	{
		if($this->db->insert('approval_sd', $data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	function get($id_project = null) 
	{
		$this->db->select()->from('project_thread');
		if ($id_project != null) 
		{
			$this->db->where('id_project_parent', $id_project);
			$this->db->order_by('id_sd_thread');
		}
		else 
		{
			$this->db->order_by('id_sd_thread');
		}
 
		$query = $this->db->get();
 
			return $query->result_array();
	}
	
	function getApproval($idThread = null) 
	{
		$this->db->select()->from('approval_sd');
		if ($idThread != null) 
		{
			$this->db->where('id_sd_thread', $idThread);
			$this->db->order_by('id_approval');
		}
		else 
		{
			$this->db->order_by('id_approval');
		}
 
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function getThread($idThread) 
	{
		$this->db->select()->from('project_thread');
		if ($idThread != null) 
		{
			$this->db->where('id_sd_thread', $idThread);
			$this->db->order_by('id_sd_thread');
		}
		else 
		{
			$this->db->order_by('id_sd_thread');
		}
 
		$query = $this->db->get();
		return $query->row_array();
	}
	
	
	function getParentProject() 
	{
		$this->db->select()->from('project_parent');
		$this->db->order_by('id_project');
 
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function getPost($idThread) 
	{
		$query = $this->db->query("SELECT t.id_sd_thread, p.id_sd_post, p.isi_sd_post, p.tanggal, p.username FROM project_post p, project_thread t
									WHERE t.id_sd_thread='$idThread' AND p.id_sd_thread='$idThread'");
		if ($idThread != null) 
		{
			return $query->result_array();
		}
		else 
		{
			return $query->result_array();
		}
	}
	
	function getFiles() 
	{
		$query = $this->db->query("SELECT id_sd_thread, nama_file FROM project_file");
		return $query->result_array();
	}
	
	function updateSD($data)
	{
		if($data['level'] == 3)
		{
			$kolomApproved = "approved_by_konsultan";
			$kolomVerifier = "verifier_konsultan";
			$status = $data['approved_by_konsultan'];
			$username = $data['verifier_konsultan'];
		}
		elseif($data['level'] == 4)
		{
			$kolomApproved = "approved_by_mp";
			$kolomVerifier = "verifier_mp";
			$status = $data['approved_by_mp'];
			$username = $data['verifier_mp'];
		}
		else
		{
			echo die();
		}
		$setStatus = $data['status'];
		$idThread = $data['id_sd_thread'];
		$query = $this->db->query("UPDATE project_thread SET $kolomApproved='$status', status='$setStatus', $kolomVerifier='$username' WHERE id_sd_thread='$idThread'");
		//var_dump($data);
		//die();
	}
	
	public function is_having_file($idPost)
	{
		$query = $this->db->get_where('project_file', array('id_sd_post' => $idPost));
		if($query->num_rows() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function update($username,$data)
	{
		$this->db->where('username', $username);
		$this->db->update('users', $data); 
	}
	
	
	public function delete($username)
	{
		$this->db->delete('users', array('username' => $username) ); 
	}
	
	//INSERT INSPECTION GUIDE
	public function insertIG($data)
	{
		if($this->db->insert('project_ig', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	public function insert_question($data)
	{
		if($this->db->insert('list_question', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	
	function getIG($idThread) 
	{
		$this->db->select()->from('project_ig');
		if ($idThread != null) 
		{
			$this->db->where('id_sd_thread', $idThread);
			$this->db->order_by('id_sd_thread');
		}
		else 
		{
			$this->db->order_by('id_sd_thread');
		}
 
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function getQuestion() 
	{
		$query = $this->db->query("SELECT * FROM list_question WHERE parent=0");
		return $query->result_array();
	}
	
	function getChildQuestion() 
	{
		$query = $this->db->query("SELECT * FROM list_question WHERE parent!=0");
		return $query->result_array();
	}
	
	function is_having_ig($idThread)
	{
		$query = $this->db->query("SELECT * FROM project_ig WHERE id_sd_thread='$idThread'");
		return $query->num_rows();
	}
	//END INSPECTION GUIDE
	
	//INSERT Request Of Work
	public function insertROW($data)
	{
		if($this->db->insert('project_row', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	public function insertROWDetails($data)
	{
		if($this->db->insert('row_details', $data))
		{
			return $this->db->insert_id();
		}
		else
		{
			return false;
		}
	}
	public function insertROWFiles($data)
	{
		if($this->db->insert('row_files', $data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function getROW($idThread) 
	{
		$query = $this->db->query("SELECT * FROM project_row WHERE id_sd_thread='$idThread'");
		return $query->result_array();
	}
	

	function getROWByID($idROW) 
	{
		$query = $this->db->query("SELECT pr.*, pt.id_project_parent FROM project_row pr, project_thread pt WHERE id_row='$idROW' AND pr.id_sd_thread=pt.id_sd_thread");
		return $query->result_array();
	}
	
	function getROWDetail($idROW) 
	{
		$query = $this->db->query("SELECT * FROM row_details WHERE id_row='$idROW'");
		return $query->result_array();
	}
	
	function getROWFiles($idROW) 
	{
		$query = $this->db->query("SELECT * FROM row_files WHERE id_row='$idROW'");
		return $query->result_array();
	}
	

	function is_having_row($idThread)
	{
		$query = $this->db->query("SELECT * FROM project_row WHERE approved_by_konsultan=''");
		return $query->result_array();
	}
	
	function updateROW($data)
	{
		if($data['level'] == 3)
		{
			$kolomApproved = "approved_by_konsultan";
			$kolomVerifier = "verifier_konsultan";
			$status = $data['approved_by_konsultan'];
			$username = $data['verifier_konsultan'];
		}
		elseif($data['level'] == 4)
		{
			$kolomApproved = "approved_by_mp";
			$kolomVerifier = "verifier_mp";
			$status = $data['approved_by_mp'];
			$username = $data['verifier_mp'];
		}
		else
		{
			echo die();
		}
		$setStatus = $data['status'];
		$idROW = $data['id_row'];
		$query = $this->db->query("UPDATE project_row SET $kolomApproved='$status', row_status='$setStatus', $kolomVerifier='$username' WHERE id_row='$idROW'");
		//var_dump($data);
		//die();
	}
	//END Request Of Work
}
?>